<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    function isPromise (val) {
      return typeof val.then === 'function'; // (123).then => undefined
    }

    Promise.allSettled = function(promises) {
      return new Promise((resolve, reject) => {
        debugger
        let arr = [];
        let times = 0;
        const setData = (index, data) => {
          arr[index] = data;
          if (++times === promises.length) {
            resolve(arr);
          }
        }

        for (let i = 0; i < promises.length; i++) {
          let current = promises[i];
          if (isPromise(current)) {
            current.then((data) => {
              setData(i, { status: 'fulfilled', value: data });
            }, err => {
              setData(i, { status: 'rejected', value: err })
            })
          } else {
            setData(i, { status: 'fulfilled', value: current })
          }
        }
      })
    }
    let p1 = Promise.resolve(123)
    let p2 = Promise.reject(456)  
    let p3 = Promise.resolve(new Error('789'))
    Promise.allSettled([p1, p2, p3]).then(data => {
      console.log(data, 'data')
    }).catch(err => {
      console.log(err, 'err')
    })
  </script>
</body>
</html>